package org.example.text;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.List;
import java.util.Set;

/**
 * @Auther:Liu
 * @Testname:ZSetText
 * @Date:2025/8/12 9:32
 */
@SpringBootTest
@Slf4j
public class ZSetText {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void a(){
        ZSetOperations<String, String> ops = stringRedisTemplate.opsForZSet();
       ops.add("A", "1", 2.3);
       ops.add("A", "2", 6.3);
       ops.add("A", "3", 0.3);
       ops.add("A", "12", 1.3);
       ops.add("A", "5", 5.3);
        ops.add("B", "1", 2.3);
        ops.add("B", "2", 6.3);
        ops.add("B", "3", 0.3);
        ops.add("B", "4", 1.3);
        ops.add("B", "6", 5.3);
        ops.addIfAbsent("A", "1", 2.3);
    }

    @Test
    public void b(){
        ZSetOperations<String, String> ops = stringRedisTemplate.opsForZSet();
        Set<String> a = ops.range("A", 0, -1);
        log.info("a={}", a);
        String s = ops.randomMember("A");
        log.info("s={}", s);
        List<String> randomMembers = ops.randomMembers("A", 4);
        log.info("randomMembers={}", randomMembers);
    }

    @Test
    public void c(){
        ZSetOperations<String, String> ops = stringRedisTemplate.opsForZSet();
        Set<String> difference = ops.difference("A", "B");
        log.info("difference={}", difference);
        Set<String> intersect = ops.intersect("A", "B");
        log.info("intersect={}", intersect);
        Set<String> union = ops.union("A", "B");
        log.info("union={}", union);
        ZSetOperations.TypedTuple<String> b = ops.popMax("B");
        log.info("b={}", b);
        ZSetOperations.TypedTuple<String> a = ops.popMin("A");
        log.info("a={}", a);
        Long size = ops.size("A");
        log.info("size={}", size);
    }
}
